package com.jdk.learn.thread;

import org.apache.log4j.Logger;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RSet;
import org.redisson.config.Config;

import java.util.concurrent.TimeUnit;

/**
 * Config config = new Config();
 * config.useSingleServer().setAddress("127.0.0.1:6379");
 * redisson = (Redisson) Redisson.create(config);
 * _log.info(redisson.getConfig().toJSON());
 */
public class DLock{
    private static final Logger _log = Logger.getLogger(DLock.class);
    public static Redisson redisson;

    static {
        Config config = new Config();
        config.useSingleServer().setAddress("127.0.0.1:6379");
        redisson = (Redisson) Redisson.create(config);
        System.out.println("初始化");
    }
    /**
     * 获取默认锁（可能死锁）
     */
    public static RLock acquireLock(String lockName) {
        RLock lock = redisson.getLock(lockName);
        lock.lock();
        System.out.println("获取锁 - " + lockName + "成功");
        return lock;
    }

    /**
     * 释放默认锁
     */
    public static void realeaseLock(RLock fairLock) {
        fairLock.unlock();
        System.out.println("释放锁 - " + fairLock.getName() + " - 成功");
    }

    /**
     * 获取非公平锁
     * @param lockName
     * @param time 多长后自动解锁（单位：分）
     */
    public static RLock acquireLockMinute(String lockName, long time) {
        RLock lock = redisson.getLock(lockName);
        lock.lock(time, TimeUnit.MINUTES);
        _log.info("获取锁 - " + lockName + "成功");
        return lock;
    }

    /**
     * 获取公平锁
     */
    public static RLock acquireFairLock(String lockName) {
        RLock fairLock = redisson.getFairLock(lockName);
        fairLock.lock();
        return fairLock;
    }

    /**
     * 释放公平锁
     */
    public static void realeaseFairLock(RLock fairLock) {
        fairLock.unlock();
    }

    /**
     * 获取读写锁 true 是读锁
     */
    public static RReadWriteLock acquireReadWrite(String lockName, boolean isRead) {
        RReadWriteLock rwlock = redisson.getReadWriteLock(lockName);
        if (isRead) {
            rwlock.readLock().lock();
            // rwlock.readLock().lock(10, TimeUnit.SECONDS);
        } else {
            rwlock.writeLock().lock();
            // rwlock.writeLock().lock(10, TimeUnit.SECONDS);
        }
        _log.error("======lock======" + Thread.currentThread().getName());
        return rwlock;
    }

    /**
     * 释放读写锁 true 是读锁
     */
    public static void releaseReadWrite(RReadWriteLock rwlock, boolean isRead) {
        if (isRead) {
            rwlock.readLock().unlock();

        } else {
            rwlock.writeLock().unlock();
        }
        _log.error("======lock======" + Thread.currentThread().getName());
    }

    /**
     * 存入分布式set对象中
     * @param name
     * @param value
     * @return
     */
    public static RSet<String> setSet(String name, String value){
        RSet<String> set = redisson.getSet(name);
        set.add(value);
        return  set;
    }

    /**
     * 检查分布式对象是否存在
     * @param name
     * @return
     */
    public static Boolean existSet(String name){
        RSet<String> set = redisson.getSet(name);
        if (!set.isEmpty()) {
            return  true;
        }
        return  false;
    }




}
